#include <iostream>
#include "logger.hpp"
#include "util.hpp"
#include "db.hpp"
#include "online.hpp"
#include "room.hpp"

using std::string;
using std::cout;
using std::endl;
using std::vector;

#define HOST "127.0.0.1"
#define USER "root"
#define PASS "LJb147258"
#define DBNAME "gobang"
#define PORT 3306

////////////////////////////测试函数/////////////////////////////
void mysql_test();//myqsl_util测试
void json_test();//json_util测试
void str_test();//string_util测试
void file_test();//文件读取测试
void db_test();//用户信息的增删查改的测试
void online_test();//用户在线管理
void room_test();//用户房间管理的测试
void room_manager_test();//房间管理的测试
///////////////////////////////////////////////////////////////

int main()
{
    
    //mysql_test();
    //json_test();
    //str_test();
    //file_test();
    //db_test();
    //online_test();
    //room_test();
    room_manager_test();
    return 0;
}

void room_manager_test()//房间管理的测试
{
    user_table ut(HOST,USER,PASS,DBNAME,PORT);
    online_manager om;
    room_manager rm(&ut,&om);
    rm.create_room(10,20);
}

void room_test()//用户房间管理的测试
{
    user_table ut(HOST,USER,PASS,DBNAME,PORT);
    online_manager om;
    room r(10,&ut,&om);

}

void online_test()//用户在线管理
{
    online_manager om;
    wsserver_t::connection_ptr conn;
    uint64_t uid = 2;

    om.enter_geam_room(uid,conn);
    if(om.is_in_game_room(uid))
    {
        DLOG("IN GAME room");
    }
    else
    {
        DLOG("NOT IN GAME room");
    }

    om.exit_game_room(uid);

    if(om.is_in_game_room(uid))
    {
        DLOG("IN GAME room");
    }
    else
    {
        DLOG("NOT IN GAME room");
    }

    // om.enter_geam_hall(uid,conn);
    // if(om.is_in_game_hall(uid))
    // {
    //     DLOG("IN GAME HALL");
    // }
    // else
    // {
    //     DLOG("NOT IN GAME HALL");
    // }

    // om.exit_game_hall(uid);

    // if(om.is_in_game_hall(uid))
    // {
    //     DLOG("IN GAME HALL");
    // }
    // else
    // {
    //     DLOG("NOT IN GAME HALL");
    // }
}


//mysql_util测试
void mysql_test()
{
    MYSQL* mysql = mysql_util::mysql_create(HOST,USER,PASS,DBNAME,PORT);

    std::string sq1 = "insert stu values(null,'小黑',18,53,68,87);";

    bool ret = mysql_util::mysql_exec(mysql,sq1);
    if(ret == 0)
    {
        return;
    }
    mysql_util::mysql_destroy(mysql);
}

//json_util测试
void json_test()
{
    std::string body;
    Json::Value root;
    root["name"] = "xiaoming";
    root["age"] = 18;
    root["score"] .append(98);
    root["score"] .append(87.5);
    root["score"] .append(78.5);
    json_util::serialize(root,body);
    DLOG("%s",body.c_str());

    Json::Value val;
    json_util::unserialize(body,val);

    std::cout << "name:" << root["name"].asCString() << std::endl;
    std::cout << "age:" << root["age"].asInt() << std::endl;
    int sz = root["score"].size();
    for(int i = 0; i < sz; i++)
    {
        std::cout << "score:" << root["score"][i].asFloat() << std::endl;
    }

}

//string_util测试
void str_test()
{
    string str = ",,,,,123,456,789,,,,,234,345,,,,,,";
    std::vector<std::string> res;
    string_util::split(str, "," ,res);

    for(auto& e:res)
    {
        cout << e << endl;
    }

}

//文件读取测试
void file_test()
{
    std::string filename = "./makefile";
    std::string res;
    file_util::read(filename,res);

    cout << res << endl;
}


//用户信息的增删查改的测试
void db_test()
{
    user_table ut(HOST,USER,PASS,DBNAME,PORT);

    Json::Value user;
    user["username"] = "xiaoming";
    //user["password"] = "123456";

    ut.login(user);
    // bool ret = ut.lose(1);

    // std::string body;
    // json_util::serialize(user,body);

    // cout << body << endl;

    // if(ret == false)
    // {
    //     DLOG("LOGIN FALSE!!!!");
    // }
}


